<?php
namespace home\Model;
use Think\Model;
class GoodsModel extends Model
{
    public function parse_good($data,$user_id="",$type="info"){
        $data['specs'] = json_decode($data['specs'],true);
        $data['imgs'] = json_decode($data['imgs'],true);
        $data['videos'] = json_decode($data['videos'],true);
        $data['input'] = json_decode($data['input'],true);
        $data['output'] = json_decode($data['output'],true);
        $data['results'] = json_decode($data['results'],true);
        $data['tags'] = json_decode($data['tags'],true);

        if ($data['line_type'] == 1) {
            $data['line_type_str'] = "驻场服务";
        }else if ($data['line_type'] == 2) {
            $data['line_type_str'] = "云服务";
        }else if ($data['line_type'] == 3) {
            $data['line_type_str'] = "驻场服务+云服务";
        }

        $data['user'] = M('user as a')->join(' left join city as c on a.city_id = c.id')->field('a.* , c.name as city_name')->where('a.id = '.$data['user_id'])->find();
        //$data['user'] = D('user')->parse_user($data['user']);
        unset($data['user']['token']);
        $data['user']['good_id'] =$data['id'];

        if ($type == "info") {
            # code...
            if ($user_id != ""){
                $data['user']['is_collection_shop'] =
                    M('collection')->where('user_id = '.$user_id.' and object_id = '.$data['user_id'].' and type = 5')->count();

                $data['user']['is_collection_good']=
                    M('collection')->where('user_id = '.$user_id.' and object_id = '.$data['id'].' and type = 6')->count();
            }
        }

        if ($data['status'] == 1) {
            $data['status_name'] = "待审核";
        }else if ($data['status'] == 2){
            $data['status_name'] = "正常";
        }

        if ($data['is_show'] == 1) {
            $data['is_show'] = true;
        }else if ($data['is_show'] == 0){
            $data['is_show'] = false;
        }

        if ($data['need_stock'] == 1) {
            $data['need_stock'] = true;
        }else{
            $data['need_stock'] = false;
            $data['stock'] = "————";
        }

        if ($data['need_specs'] == 1) {
            
            $max_price = 0;
            $min_price = 1000000000;

            foreach ($data['specs'] as $key => $value) {
                if ($max_price <= $data['specs'][$key]['price']) {
                    $max_price = $data['specs'][$key]['price'];
                }

                if ($min_price >= $data['specs'][$key]['price']) {
                    $min_price = $data['specs'][$key]['price'];
                }
            }
            $data['price'] = $min_price."~".$max_price;
            $data['need_specs'] = true;
            $data['stock'] = "————";
        }else{
            $data['need_specs'] = false;
            $data['specs_name'] = false;
        }

        $data['score'] = 4.5;
        return $data;
    }

    public function parse_goods($data,$user_id=""){

        foreach ($data as $key => $value) {
            $data[$key] = $this->parse_good($data[$key],$user_id,"list");
        }

        return $data;
    }
}